#!/bin/sh
#

: << =cut

=head1 NAME

wmr_all_weather - Plugin to monitor WMR Oregon all weather sensors data...

=head1 CONFIGURATION

The following enviroment variables are used by this plugin:

 wmrconf - full path to config file to use. (Default /etc/wmr/wmr.conf)

=head1 USAGE

Place the plugin in the /etc/munin/plugins/ directory, and make it
executable.

=head1 AUTHOR

Oregon Scientific WMR100/200/WMRS200/I300/I600/RMS600 protocol.
Latest download URL: http://www.nkl.ru/support/wmr/
Global download URL: http://code.google.com/p/wmr/

Copyright (C) 2012 @Den68 <idg68@yandex.ru>

=cut

wmrconf=${wmrconf:-/etc/wmr/wmr.conf}

        if [ ! -x "`which sqlite3`" ]; then
                echo "no '(sqlite3 not found)'"
                exit 0
	fi

	if [ ! -f $wmrconf ]; then
                echo "no '(" $wmrconf " not found)'"
                exit 0
	fi

SQLBASEPATH=`cat $wmrconf | grep SQLBASEPATH | awk '{ print $2 }'`
echo -n "humid_in:"`sqlite3 -column ${SQLBASEPATH} "select humidity from TEMP where sensor = 0 ORDER BY tdate DESC LIMIT 1"`
echo -n " humid_out:"`sqlite3 -column ${SQLBASEPATH} "select humidity from TEMP where sensor = 1 ORDER BY tdate DESC LIMIT 1"`
echo -n " pressure:"`sqlite3 -column ${SQLBASEPATH} "select pressure from PRESSURE ORDER BY tdate DESC LIMIT 1"`
echo -n " rain_total:"`sqlite3 -column ${SQLBASEPATH} "select all_total from RAIN ORDER BY tdate DESC LIMIT 1"`
echo -n " rain_24h":`sqlite3 -column ${SQLBASEPATH} "select day_total from RAIN ORDER BY tdate DESC LIMIT 1"`
echo -n " rain_1h:"`sqlite3 -column ${SQLBASEPATH} "select hour_total from RAIN ORDER BY tdate DESC LIMIT 1"`
echo -n " rain_rate:"`sqlite3 -column ${SQLBASEPATH} "select rate from RAIN ORDER BY tdate DESC LIMIT 1"`
echo -n " temp_in:"`sqlite3 -column ${SQLBASEPATH} "select temp from TEMP where sensor = 0 ORDER BY tdate DESC LIMIT 1"`
echo -n " temp_out:"`sqlite3 -column ${SQLBASEPATH} "select temp from TEMP where sensor = 1 ORDER BY tdate DESC LIMIT 1"`
echo -n " temp_dew_in:"`sqlite3 -column ${SQLBASEPATH} "select dewpoint from TEMP where sensor = 0 ORDER BY tdate DESC LIMIT 1"`
echo -n " temp_dew_out:"`sqlite3 -column ${SQLBASEPATH} "select dewpoint from TEMP where sensor = 1 ORDER BY tdate DESC LIMIT 1"`
echo -n " uv_index:"`sqlite3 -column ${SQLBASEPATH} "select uvindex from UV ORDER BY tdate DESC LIMIT 1"`
echo -n " temp_w:"`sqlite3 -column ${SQLBASEPATH} "select temp from WATER where sensor = 0 ORDER BY tdate DESC LIMIT 1"`
echo -n " wind_speed:" `sqlite3 -column ${SQLBASEPATH} "select speed from WIND ORDER BY tdate DESC LIMIT 1"`
echo -n " wind_gust:" `sqlite3 -column ${SQLBASEPATH} "select avgspeed from WIND ORDER BY tdate DESC LIMIT 1"`
echo -n " wind_dir:" `sqlite3 -column ${SQLBASEPATH} "select dir from WIND ORDER BY tdate DESC LIMIT 1"`
